﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace lcs.Web.Outstorage
{
    public partial class ShowCheckDetail : Web.Admin.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //待审核出库项目
                //if (Session["proid"] != null && Session["login"] != null)
                if (Session["out_storagelist_batchid"] != null && Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    string out_storagelist_batchid = Session["out_storagelist_batchid"].ToString();
                    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                    //BindGV(proid,user_id,"未审核");
                    BindGV(out_storagelist_batchid);
                }
                else if (Request["out_storagelist_batchid"] != null && Session["login"] != null)//已审核通过出库项目
                {
                    DataSet ds = Session["login"] as DataSet;
                    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                    string out_storagelist_batchid = Request["out_storagelist_batchid"];

                    //BindGV(proid, user_id, "审核通过");
                    BindGV(out_storagelist_batchid);
                    this.btn_Yes.Visible = false;
                    this.btn_No.Visible = false;
                }
                else
                {
                    Maticsoft.Common.MessageBox.Show(this, "由于长时间未操作，登录信息已过期，请重新登陆!");
                }
            }
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            //if (Session["proid"] != null && Session["login"] != null)
            if (Session["out_storagelist_batchid"] != null && Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                string out_storagelist_batchid = Session["out_storagelist_batchid"].ToString();
                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                //BindGV(proid, user_id, "未审核");
                BindGV(out_storagelist_batchid);
            }
            else
            {
                Maticsoft.Common.MessageBox.Show(this, "由于长时间未操作，登录信息已过期，请重新登陆!");
            }
           
        }
        
        //审核通过
        protected void btn_Yes_Click(object sender, EventArgs e)
        {
            //List<lcs.Model.out_storageList> li = this.GetCboList();

            //lcs.BLL.out_storageList om = new BLL.out_storageList();

            //foreach (lcs.Model.out_storageList o in li)
            //{
            //    o.check_status = "审核通过";
            //    o.check_reason = this.txt_reason.Text;
            //    om.Update(o);
            //}

            //ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.returnValue=true;parent.close();</script>");


            if (Session["out_storagelist_batchid"] != null)
            {
                string dpid = Session["out_storagelist_batchid"].ToString();

                string reason = this.txt_reason.Text;

                if (string.IsNullOrEmpty(reason))
                {
                    reason = "同意";
                }


                int person_id = 0;
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    person_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"]);
                }

                //同意处理
                lcs.BLL.Tools.DealAgree_OutSumbit(dpid, person_id, reason);

                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.returnValue=true;parent.close();</script>");
            }

        }

        //审核未通过
        protected void btn_No_Click(object sender, EventArgs e)
        {
            //List<lcs.Model.out_storageList> li = this.GetCboList();

            //lcs.BLL.out_storageList om = new BLL.out_storageList();

            //foreach (lcs.Model.out_storageList o in li)
            //{
            //    o.check_status = "审核未通过";
            //    o.check_reason = this.txt_reason.Text;
            //    om.Update(o);
            //}

            //ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.returnValue=true;parent.close();</script>");

            if (Session["out_storagelist_batchid"] != null)
            {
                string dpid = Session["out_storagelist_batchid"].ToString();

                string reason = this.txt_reason.Text;

                if (string.IsNullOrEmpty(reason))
                {
                    reason = "否决";
                }


                int person_id = 0;
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    person_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"]);
                }

                //返回上一步
                lcs.BLL.Tools.DealDisagreeLast_OutSubmit(dpid, person_id, reason);

                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.returnValue=true;parent.close();</script>");
            }


        }

        private List<lcs.Model.out_storageList> GetCboList()
        {
            List<lcs.Model.out_storageList> li = new List<Model.out_storageList>();

            lcs.BLL.out_storageList om = new BLL.out_storageList();

            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox cbo = this.GridView1.Rows[i].FindControl("cbo_check") as CheckBox;

                if (cbo != null && cbo.Checked)
                {

                    Label lbl_out_storageList_id = this.GridView1.Rows[i].FindControl("lbl_out_storageList_id") as Label;//物料ID

                    int oid = 0;
                    if (!string.IsNullOrEmpty(lbl_out_storageList_id.Text))
                    {
                        oid = Convert.ToInt32(lbl_out_storageList_id.Text);
                    }

                    lcs.Model.out_storageList o = om.GetModel(oid);

                    li.Add(o);
                }
            }

            return li;
        }


        //private void BindGV(string proid, int check_user_id,string check_status)
        private void BindGV(string out_storagelist_batchid)
        {
            //DataSet ds = new lcs.BLL.out_storageList().GetCheckUserDetail
            //    (" proid='" + proid + "' and check_user_id=" + check_user_id + " and check_status='" + check_status + "'");

            DataSet ds = new lcs.BLL.out_storageList().GetCheckUserDetail(" out_storagelist_batchid='"+ out_storagelist_batchid + "' ");

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }
    }
}